
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Basics · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="changesets.html" />
    
    
    <link rel="prev" href="../advanced/nerves.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        <li class="header">第一部分：基础</li>
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../basics/basics.html">
            
                <a href="../basics/basics.html">
            
                    
                    基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../basics/collections.html">
            
                <a href="../basics/collections.html">
            
                    
                    集合
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../basics/enum.html">
            
                <a href="../basics/enum.html">
            
                    
                    Enum 模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../basics/pattern-matching.html">
            
                <a href="../basics/pattern-matching.html">
            
                    
                    模式匹配
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../basics/control-structures.html">
            
                <a href="../basics/control-structures.html">
            
                    
                    控制语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../basics/functions.html">
            
                <a href="../basics/functions.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../basics/pipe-operator.html">
            
                <a href="../basics/pipe-operator.html">
            
                    
                    管道操作符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../basics/modules.html">
            
                <a href="../basics/modules.html">
            
                    
                    模块(Module)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../basics/mix.html">
            
                <a href="../basics/mix.html">
            
                    
                    Mix
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="../basics/sigils.html">
            
                <a href="../basics/sigils.html">
            
                    
                    魔符(Sigil)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="../basics/documentation.html">
            
                <a href="../basics/documentation.html">
            
                    
                    文档模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="../basics/testing.html">
            
                <a href="../basics/testing.html">
            
                    
                    测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="../basics/comprehensions.html">
            
                <a href="../basics/comprehensions.html">
            
                    
                    推导
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="../basics/strings.html">
            
                <a href="../basics/strings.html">
            
                    
                    字符串
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="../basics/date-time.html">
            
                <a href="../basics/date-time.html">
            
                    
                    日期和时间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="../basics/mix-tasks.html">
            
                <a href="../basics/mix-tasks.html">
            
                    
                    自定义Mix任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="../basics/iex-helpers.html">
            
                <a href="../basics/iex-helpers.html">
            
                    
                    IEx辅助函数
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第二部分：高级</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../advanced/erlang.html">
            
                <a href="../advanced/erlang.html">
            
                    
                    和Erlang互操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="../advanced/error-handling.html">
            
                <a href="../advanced/error-handling.html">
            
                    
                    错误处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="../advanced/escripts.html">
            
                <a href="../advanced/escripts.html">
            
                    
                    可执行文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="../advanced/concurrency.html">
            
                <a href="../advanced/concurrency.html">
            
                    
                    并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="../advanced/otp-concurrency.html">
            
                <a href="../advanced/otp-concurrency.html">
            
                    
                    OTP并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="../advanced/otp-supervisors.html">
            
                <a href="../advanced/otp-supervisors.html">
            
                    
                    OTP Supervisors
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="../advanced/otp-distribution.html">
            
                <a href="../advanced/otp-distribution.html">
            
                    
                    OTP 分布式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="../advanced/metaprogramming.html">
            
                <a href="../advanced/metaprogramming.html">
            
                    
                    元编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.9" data-path="../advanced/umbrella-projects.html">
            
                <a href="../advanced/umbrella-projects.html">
            
                    
                    Umbrella Projects
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.10" data-path="../advanced/typespec.html">
            
                <a href="../advanced/typespec.html">
            
                    
                    Specifications and types
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.11" data-path="../advanced/behaviours.html">
            
                <a href="../advanced/behaviours.html">
            
                    
                    行为
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.12" data-path="../advanced/gen-stage.html">
            
                <a href="../advanced/gen-stage.html">
            
                    
                    GenStage
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.13" data-path="../advanced/protocols.html">
            
                <a href="../advanced/protocols.html">
            
                    
                    协议
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.14" data-path="../advanced/nerves.html">
            
                <a href="../advanced/nerves.html">
            
                    
                    Nerves
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第三部分：ECTO</li>
        
        
    
        <li class="chapter active" data-level="3.1" data-path="basics.html">
            
                <a href="basics.html">
            
                    
                    Basics
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="changesets.html">
            
                <a href="changesets.html">
            
                    
                    Changesets
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="associations.html">
            
                <a href="associations.html">
            
                    
                    关联关系
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="querying.html">
            
                <a href="querying.html">
            
                    
                    查询
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第四部分：专题</li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="../specifics/plug.html">
            
                <a href="../specifics/plug.html">
            
                    
                    Plug
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="../specifics/eex.html">
            
                <a href="../specifics/eex.html">
            
                    
                    嵌入的 Elixir(EEx)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="../specifics/ets.html">
            
                <a href="../specifics/ets.html">
            
                    
                    Erlang 项式存储(ETS)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="../specifics/mnesia.html">
            
                <a href="../specifics/mnesia.html">
            
                    
                    Mnesia 数据库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="../specifics/debugging.html">
            
                <a href="../specifics/debugging.html">
            
                    
                    调试
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第五部分：程序库</li>
        
        
    
        <li class="chapter " data-level="5.1" data-path="../libraries/guardian.html">
            
                <a href="../libraries/guardian.html">
            
                    
                    Guardian(基础)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="../libraries/poolboy.html">
            
                <a href="../libraries/poolboy.html">
            
                    
                    Poolboy
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="../libraries/benchee.html">
            
                <a href="../libraries/benchee.html">
            
                    
                    Benchee
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="../libraries/bypass.html">
            
                <a href="../libraries/bypass.html">
            
                    
                    Bypass
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="../libraries/distillery.html">
            
                <a href="../libraries/distillery.html">
            
                    
                    Distillery(基础)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="../libraries/stream-data.html">
            
                <a href="../libraries/stream-data.html">
            
                    
                    StreamData
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >Basics</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <p>Ecto &#x662F; Elixir &#x5B98;&#x65B9;&#x63D0;&#x4F9B;&#x7684;&#x6570;&#x636E;&#x5E93;&#x5305;&#x88C5;&#x548C;&#x96C6;&#x6210;&#x67E5;&#x8BE2;&#x8BED;&#x8A00;&#x7684;&#x9879;&#x76EE;&#x3002; &#x4F7F;&#x7528;Ecto&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x521B;&#x5EFA; migrations&#xFF0C;&#x5B9A;&#x4E49; schema&#xFF0C;&#x63D2;&#x5165;&#x548C;&#x66F4;&#x65B0;&#x8BB0;&#x5F55;&#xFF0C;&#x4EE5;&#x53CA;&#x67E5;&#x8BE2;&#x6570;&#x636E;&#x3002;</p>
<h3 id="&#x9002;&#x914D;&#x5668;">&#x9002;&#x914D;&#x5668;</h3>
<p>Ecto&#x901A;&#x8FC7;&#x4F7F;&#x7528;&#x9002;&#x914D;&#x5668;&#x53EF;&#x4EE5;&#x652F;&#x6301;&#x4E0D;&#x540C;&#x7684;&#x6570;&#x636E;&#x5E93;&#x3002; &#x4E00;&#x4E9B;&#x5E38;&#x89C1;&#x7684;&#x9002;&#x914D;&#x5668;&#x7684;&#x6BD4;&#x5982;&#x8BF4;&#xFF1A;</p>
<ul>
<li>PostgreSQL</li>
<li>MySQL</li>
<li>SQLite</li>
</ul>
<p>&#x5728;&#x672C;&#x8BFE;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x5C06;&#x4F7F;&#x7528; PostgreSQL &#x9002;&#x914D;&#x5668;&#x6765;&#x914D;&#x7F6E; Ecto&#x3002;</p>
<h3 id="&#x5F00;&#x59CB;">&#x5F00;&#x59CB;</h3>
<p>&#x5728;&#x672C;&#x8BFE;&#x7A0B;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x5C06;&#x4ECB;&#x7ECD;&#x6709;&#x5173; Ecto &#x7684;&#x4EE5;&#x4E0B;&#x4E09;&#x90E8;&#x5206;&#xFF1A;</p>
<ul>
<li>Repository - &#x63D0;&#x4F9B;&#x6570;&#x636E;&#x5E93;&#x7684;&#x63A5;&#x53E3;&#xFF0C;&#x5305;&#x62EC;&#x8FDE;&#x63A5;</li>
<li>Migrations - &#x4E00;&#x79CD;&#x521B;&#x5EFA;&#xFF0C;&#x4FEE;&#x6539;&#x548C;&#x5220;&#x9664;&#x6570;&#x636E;&#x5E93;&#x8868;&#x548C;&#x7D22;&#x5F15;&#x7684;&#x673A;&#x5236;</li>
<li>Schemas - &#x8868;&#x793A;&#x6570;&#x636E;&#x5E93;&#x8868;&#x5B9E;&#x4F8B;&#x7684;&#x7279;&#x6B8A;&#x7ED3;&#x6784;</li>
</ul>
<p>&#x9996;&#x5148;&#xFF0C;&#x8BA9;&#x6211;&#x4EEC;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x5E26;&#x6709; supervision &#x6811;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;</p>
<pre><code class="lang-shell">$ mix new friends --sup
$ cd friends
</code></pre>
<p>&#x5C06; ecto &#x548C; postgrex &#x5305;&#x4F9D;&#x8D56;&#x9879;&#x6DFB;&#x52A0;&#x5230; <code>mix.exs</code> &#x6587;&#x4EF6;&#x4E2D;</p>
<pre><code class="lang-elixir">  <span class="hljs-function"><span class="hljs-keyword">defp</span> <span class="hljs-title">deps</span></span> <span class="hljs-keyword">do</span>
    [
      {<span class="hljs-symbol">:ecto</span>, <span class="hljs-string">&quot;~&gt; 2.0&quot;</span>},
      {<span class="hljs-symbol">:postgrex</span>, <span class="hljs-string">&quot;~&gt; 0.11&quot;</span>}
    ]
  <span class="hljs-keyword">end</span>
</code></pre>
<p>&#x4F7F;&#x7528;&#x5982;&#x4E0B;&#x547D;&#x4EE4;&#x5B89;&#x88C5;&#x4F9D;&#x8D56;&#x9879;</p>
<pre><code class="lang-shell">$ mix deps.get
</code></pre>
<h4 id="&#x521B;&#x5EFA;&#x4E00;&#x4E2A;-repository">&#x521B;&#x5EFA;&#x4E00;&#x4E2A; Repository</h4>
<p>&#x5728; Ecto &#x4E2D;&#xFF0C;&#x4E00;&#x4E2A; repository &#x662F;&#x6620;&#x5C04;&#x5230;&#x6570;&#x636E;&#x5B58;&#x50A8;&#x533A;&#x7684;&#xFF0C;&#x4F8B;&#x5982; Postgres &#x6570;&#x636E;&#x5E93;&#x3002;&#x6240;&#x6709;&#x4E0E;&#x6570;&#x636E;&#x5E93;&#x7684;&#x901A;&#x4FE1;&#x90FD;&#x5C06;&#x4F7F;&#x7528;&#x8BE5; repository &#x5B8C;&#x6210;&#x3002;</p>
<p>&#x901A;&#x8FC7;&#x8FD0;&#x884C;&#x4EE5;&#x4E0B;&#x547D;&#x4EE4;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;Repository:</p>
<pre><code class="lang-shell">$ mix ecto.gen.repo -r Friends.Repo
</code></pre>
<p>&#x4E0A;&#x8FF0;&#x547D;&#x4EE4;&#x4F1A;&#x5728; <code>config/config.exs</code> &#x6587;&#x4EF6;&#x4E2D;&#x751F;&#x6210;&#x6240;&#x9700;&#x7684;&#x914D;&#x7F6E;&#xFF0C;&#x7528;&#x6765;&#x8FDE;&#x63A5;&#x5230;&#x6211;&#x4EEC;&#x8981;&#x4F7F;&#x7528;&#x7684;&#x6570;&#x636E;&#x5E93;&#x3002;
&#x8FD9;&#x662F;&#x6211;&#x4EEC; <code>Friends</code> &#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x7684;&#x914D;&#x7F6E;&#x6587;&#x4EF6;</p>
<pre><code class="lang-elixir">config <span class="hljs-symbol">:friends</span>, Friends.Repo,
  <span class="hljs-symbol">adapter:</span> Ecto.Adapters.Postgres,
  <span class="hljs-symbol">database:</span> <span class="hljs-string">&quot;friends_repo&quot;</span>,
  <span class="hljs-symbol">username:</span> <span class="hljs-string">&quot;postgres&quot;</span>,
  <span class="hljs-symbol">password:</span> <span class="hljs-string">&quot;&quot;</span>,
  <span class="hljs-symbol">hostname:</span> <span class="hljs-string">&quot;localhost&quot;</span>
</code></pre>
<p>&#x8FD9;&#x4E2A;&#x914D;&#x7F6E;&#x51B3;&#x5B9A; Ecto &#x5982;&#x4F55;&#x8FDE;&#x63A5;&#x5230;&#x6570;&#x636E;&#x5E93;&#x3002;&#x8FD9;&#x91CC;&#x8981;&#x6CE8;&#x610F;&#x6211;&#x4EEC;&#x662F;&#x9009;&#x62E9; <code>Ecto.Adapters.Postgres</code> &#x9002;&#x914D;&#x5668;&#x3002;</p>
<p>&#x540C;&#x65F6;&#x5B83;&#x8FD8;&#x5728; <code>lib/friends/repo.ex</code> &#x6587;&#x4EF6;&#x4E2D;&#x521B;&#x5EFA;&#x4E86;&#x4E00;&#x4E2A; <code>Friends.Repo</code> &#x6A21;&#x5757;</p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">Friends</span></span>.Repo <span class="hljs-keyword">do</span>
  <span class="hljs-keyword">use</span> Ecto.Repo, <span class="hljs-symbol">otp_app:</span> <span class="hljs-symbol">:friends</span>
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x6211;&#x4EEC;&#x5C06;&#x4F7F;&#x7528; <code>Friends.Repo</code> &#x6A21;&#x5757;&#x6765;&#x67E5;&#x8BE2;&#x6570;&#x636E;&#x5E93;&#x3002; &#x6211;&#x4EEC;&#x8FD8;&#x544A;&#x8BC9;&#x8BE5;&#x6A21;&#x5757;&#x5728; <code>:friends</code> &#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x4E2D;&#x67E5;&#x627E;&#x5176;&#x5BF9;&#x5E94;&#x7684;&#x6570;&#x636E;&#x5E93;&#x914D;&#x7F6E;&#x4FE1;&#x606F;&#x3002;</p>
<p>&#x63A5;&#x4E0B;&#x6765;&#xFF0C;&#x6211;&#x4EEC;&#x5C06;&#x5728; <code>lib/friends/application.ex</code> &#x6587;&#x4EF6;&#x4E2D;&#x7684;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x7684; supervision &#x6811;&#x4E2D;&#x8BBE;&#x7F6E;<code>Friends.Repo</code>&#x4F5C;&#x4E3A; supervisor&#x3002;
&#x8FD9;&#x5C06;&#x5728;&#x6211;&#x4EEC;&#x7684;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x542F;&#x52A8;&#x65F6;&#x542F;&#x52A8;Ecto&#x8FDB;&#x7A0B;&#x3002;</p>
<pre><code class="lang-elixir">  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">start</span></span>(_type, _args) <span class="hljs-keyword">do</span>
    <span class="hljs-comment"># List all child processes to be supervised</span>
    children = [
      Friends.Repo,
    ]

  ...
</code></pre>
<p>&#x4E4B;&#x540E;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x5728; <code>config/config.exs</code> &#x6587;&#x4EF6;&#x4E2D;&#x6DFB;&#x52A0;&#x4EE5;&#x4E0B;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-elixir">config <span class="hljs-symbol">:friends</span>, <span class="hljs-symbol">ecto_repos:</span> [Friends.Repo]
</code></pre>
<p>&#x8FD9;&#x5C06;&#x5141;&#x8BB8;&#x60A8;&#x7684;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x5728;&#x547D;&#x4EE4;&#x884C;&#x4E2D;&#x8FD0;&#x884C; ecto mix &#x547D;&#x4EE4;&#x3002;</p>
<p>&#x81F3;&#x6B64;&#xFF0C;&#x6211;&#x4EEC;&#x5C31;&#x5B8C;&#x6210;&#x4E86; repository &#x914D;&#x7F6E;&#xFF01;</p>
<p>&#x6211;&#x4EEC;&#x73B0;&#x5728;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x4EE5;&#x4E0B;&#x547D;&#x4EE4;&#x5728; postgres &#x4E2D;&#x521B;&#x5EFA;&#x6570;&#x636E;&#x5E93;&#xFF1A;</p>
<pre><code class="lang-shell">$ mix ecto.create
</code></pre>
<p>Ecto &#x5C06;&#x4F7F;&#x7528; <code>config/config.exs</code> &#x6587;&#x4EF6;&#x4E2D;&#x7684;&#x914D;&#x7F6E;&#x4FE1;&#x606F;&#x6765;&#x786E;&#x5B9A;&#x5982;&#x4F55;&#x8FDE;&#x63A5;&#x5230; Postgres &#x4E2D;&#x5BF9;&#x5E94;&#x7684;&#x6570;&#x636E;&#x5E93;&#x3002;</p>
<p>&#x5982;&#x679C;&#x6536;&#x5230;&#x4EFB;&#x4F55;&#x9519;&#x8BEF;&#xFF0C;&#x8BF7;&#x786E;&#x4FDD;&#x914D;&#x7F6E;&#x4FE1;&#x606F;&#x6B63;&#x786E;&#x5E76;&#x4E14;&#x60A8;&#x7684; postgres &#x5B9E;&#x4F8B;&#x6B63;&#x5728;&#x8FD0;&#x884C;&#x3002;</p>
<h3 id="migrations">Migrations</h3>
<p>&#x8981;&#x5728; postgres &#x6570;&#x636E;&#x5E93;&#x4E2D;&#x521B;&#x5EFA;&#x548C;&#x4FEE;&#x6539;&#x8868;&#xFF0C;Ecto &#x4E3A;&#x6211;&#x4EEC;&#x63D0;&#x4F9B;&#x4E86; migrations&#x3002;
&#x6BCF;&#x4E2A; migration &#x90FD;&#x63CF;&#x8FF0;&#x4E86;&#x6211;&#x4EEC;&#x8981;&#x5BF9;&#x6570;&#x636E;&#x5E93;&#x6267;&#x884C;&#x7684;&#x4E00;&#x7EC4;&#x64CD;&#x4F5C;&#xFF0C;&#x6BD4;&#x5982;&#x8981;&#x521B;&#x5EFA;&#x6216;&#x66F4;&#x65B0;&#x7684;&#x8868;&#x3002;</p>
<p>&#x7531;&#x4E8E;&#x76EE;&#x524D;&#x6211;&#x4EEC;&#x7684;&#x6570;&#x636E;&#x5E93;&#x8FD8;&#x6CA1;&#x6709;&#x4EFB;&#x4F55;&#x8868;&#xFF0C;&#x6240;&#x4EE5;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x521B;&#x5EFA;&#x4E00;&#x4E2A; migration &#x6765;&#x6DFB;&#x52A0;&#x4E00;&#x4E9B;&#x8868;&#x3002;
Ecto &#x4E2D;&#x7684;&#x7EA6;&#x5B9A;&#x662F;&#x5C06;&#x8868;&#x683C;&#x590D;&#x6570;&#x5316;&#xFF0C;&#x56E0;&#x6B64;&#x5BF9;&#x4E8E;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x4E00;&#x4E2A; <code>people</code> &#x8868;&#xFF0C;&#x6240;&#x4EE5;&#x8BA9;&#x6211;&#x4EEC;&#x4ECE; migrations &#x5F00;&#x59CB;&#x3002;</p>
<p>&#x521B;&#x5EFA; migrations &#x7684;&#x6700;&#x4F73;&#x65B9;&#x6CD5;&#x662F;&#x6267;&#x884C; <code>mix ecto.gen.migration &lt;name&gt;</code> &#x4EFB;&#x52A1;&#xFF0C;&#x6240;&#x4EE5;&#x5728;&#x6211;&#x4EEC;&#x7684;&#x4F8B;&#x5B50;&#x4E2D;&#x4F7F;&#x7528;&#x5982;&#x4E0B;&#x547D;&#x4EE4;&#xFF1A;</p>
<pre><code class="lang-shell">$ mix ecto.gen.migration create_people
</code></pre>
<p>&#x8FD9;&#x5C06;&#x5728; <code>priv/repo/migrations</code> &#x6587;&#x4EF6;&#x5939;&#x4E2D;&#x751F;&#x6210;&#x4E00;&#x4E2A;&#x6587;&#x4EF6;&#x540D;&#x4E2D;&#x5305;&#x542B;&#x4E00;&#x4E2A;&#x65F6;&#x95F4;&#x6233;&#x7684;&#x65B0;&#x6587;&#x4EF6;&#x3002;
&#x5982;&#x679C;&#x6211;&#x4EEC;&#x6253;&#x5F00;&#x8BE5;&#x6587;&#x4EF6;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x770B;&#x5230;&#x5982;&#x4E0B;&#x5185;&#x5BB9;&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">Friends</span></span>.Repo.Migrations.CreatePeople <span class="hljs-keyword">do</span>
  <span class="hljs-keyword">use</span> Ecto.Migration

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">change</span></span> <span class="hljs-keyword">do</span>

  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x8BA9;&#x6211;&#x4EEC;&#x9996;&#x5148;&#x4FEE;&#x6539; <code>change/0</code> &#x51FD;&#x6570;&#x6765;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x5E26;&#x6709; <code>name</code> &#x548C; <code>age</code> &#x5B57;&#x6BB5;&#x7684;&#x65B0;&#x7684; <code>people</code> &#x8868;&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">Friends</span></span>.Repo.Migrations.CreatePeople <span class="hljs-keyword">do</span>
  <span class="hljs-keyword">use</span> Ecto.Migration

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">change</span></span> <span class="hljs-keyword">do</span>
    create table(<span class="hljs-symbol">:people</span>) <span class="hljs-keyword">do</span>
      add <span class="hljs-symbol">:name</span>, <span class="hljs-symbol">:string</span>, <span class="hljs-symbol">null:</span> <span class="hljs-keyword">false</span>
      add <span class="hljs-symbol">:age</span>, <span class="hljs-symbol">:integer</span>, <span class="hljs-symbol">default:</span> <span class="hljs-number">0</span>
    <span class="hljs-keyword">end</span>
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x60A8;&#x53EF;&#x4EE5;&#x5728;&#x4E0A;&#x9762;&#x770B;&#x5230;&#x6211;&#x4EEC;&#x5B9A;&#x4E49;&#x4E86;&#x5217;&#x7684;&#x6570;&#x636E;&#x7C7B;&#x578B;&#x3002;&#x53E6;&#x5916;&#xFF0C;&#x8FD8;&#x5305;&#x62EC; <code>null:false</code> &#x548C; <code>default&#xFF1A;0</code> &#x4F5C;&#x4E3A;&#x53E6;&#x5916;&#x914D;&#x7F6E;&#x9009;&#x9879;&#x3002;</p>
<p>&#x8BA9;&#x6211;&#x4EEC;&#x8DF3;&#x8F6C;&#x5230; shell &#x5E76;&#x8FD0;&#x884C;&#x6211;&#x4EEC;&#x7684; migration&#xFF1A;</p>
<pre><code class="lang-shell">$ mix ecto.migrate
</code></pre>
<h3 id="schemas">Schemas</h3>
<p>&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x5DF2;&#x7ECF;&#x521B;&#x5EFA;&#x4E86;&#x521D;&#x59CB;&#x5316;&#x7684;&#x8868;&#xFF0C;&#x63A5;&#x4E0B;&#x6765;&#x9700;&#x8981;&#x544A;&#x8BC9; Ecto &#x66F4;&#x591A;&#x5173;&#x4E8E;&#x5B83;&#x7684;&#x90E8;&#x5206;&#x5185;&#x5BB9;&#xFF0C;&#x6211;&#x4EEC;&#x5982;&#x4F55;&#x901A;&#x8FC7; schema &#x8FDB;&#x884C;&#x64CD;&#x4F5C;&#x3002;
&#x90A3;&#x4E48;&#x4EC0;&#x4E48;&#x662F; schema &#x5462;&#xFF1F;schema &#x662F;&#x5B9A;&#x4E49;&#x5E95;&#x5C42;&#x6570;&#x636E;&#x5E93;&#x8868;&#x7684;&#x5B57;&#x6BB5;&#x7684;&#x6620;&#x5C04;&#x7684;&#x6A21;&#x5757;&#x3002;</p>
<p>&#x867D;&#x7136; Ecto &#x652F;&#x6301;&#x590D;&#x6570;&#x7684;&#x6570;&#x636E;&#x5E93;&#x8868;&#x540D;&#xFF0C;&#x4F46; schema &#x901A;&#x5E38;&#x662F;&#x5355;&#x6570;&#x7684;&#xFF0C;&#x56E0;&#x6B64;&#x6211;&#x4EEC;&#x5C06;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x5305;&#x542B; <code>Person</code> &#x7684; schema &#x6765;&#x5305;&#x542B;&#x6211;&#x4EEC;&#x7684;&#x8868;&#x3002;</p>
<p>&#x8BA9;&#x6211;&#x4EEC;&#x5728; <code>lib/friends/person.ex</code> &#x6587;&#x4EF6;&#x4E2D;&#x521B;&#x5EFA;&#x65B0;&#x7684; schema:</p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">Friends</span></span>.Person <span class="hljs-keyword">do</span>
  <span class="hljs-keyword">use</span> Ecto.Schema

  schema <span class="hljs-string">&quot;people&quot;</span> <span class="hljs-keyword">do</span>
    field <span class="hljs-symbol">:name</span>, <span class="hljs-symbol">:string</span>
    field <span class="hljs-symbol">:age</span>, <span class="hljs-symbol">:integer</span>, <span class="hljs-symbol">default:</span> <span class="hljs-number">0</span>
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x5728;&#x8FD9;&#x91CC;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x770B;&#x5230; <code>Friends.Person</code> &#x6A21;&#x5757;&#x544A;&#x8BC9; Ecto &#x8FD9;&#x4E2A; schema &#x4E0E; <code>people</code> &#x8868;&#x6709;&#x5173;&#xFF0C;&#x6211;&#x4EEC;&#x6709;&#x4E24;&#x4E2A;&#x5B57;&#x6BB5;&#xFF1A;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#x7C7B;&#x578B;&#x7684; <code>name</code> &#x5B57;&#x6BB5;&#x548C;&#x4E00;&#x4E2A;<code>age</code> &#x5B57;&#x6BB5;&#xFF0C;&#x5E76;&#x6307;&#x5B9A; <code>age</code> &#x7684;&#x9ED8;&#x8BA4;&#x503C;&#x4E3A; <code>0</code>&#x3002;</p>
<p>&#x8BA9;&#x6211;&#x4EEC;&#x901A;&#x8FC7; <code>iex -S mix</code> &#x5E76;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x65B0; person &#x6765;&#x770B;&#x770B;&#x6211;&#x4EEC;&#x7684; schema &#x662F;&#x5565;&#x6837;&#x7684;&#xFF1A;</p>
<pre><code class="lang-shell">iex&gt; %Friends.Person{}
%Friends.Person{age: 0, name: nil}
</code></pre>
<p>&#x6B63;&#x5982;&#x9884;&#x671F;&#x7684;&#x90A3;&#x6837;&#xFF0C;&#x6211;&#x4EEC;&#x5F97;&#x5230;&#x4E00;&#x4E2A;&#x65B0;&#x7684;<code>age</code> &#x4E3A; 0 &#x7684; <code>Person</code> &#x7ED3;&#x6784;&#xFF0C;
&#x73B0;&#x5728;&#x8BA9;&#x6211;&#x4EEC;&#x521B;&#x5EFA;&#x4E00;&#x4E2A; &#x201C;&#x771F;&#x6B63;&#x201C; &#x7684; person&#xFF1A;</p>
<pre><code class="lang-shell">iex&gt; person = %Friends.Person{name: &quot;Tom&quot;, age: 11}
%Friends.Person{age: 11, name: &quot;Tom&quot;}
</code></pre>
<p>&#x7531;&#x4E8E; schemas &#x53EA;&#x662F;&#x7ED3;&#x6784;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x50CF;&#x4EE5;&#x524D;&#x4E00;&#x6837;&#x548C;&#x6211;&#x4EEC;&#x7684;&#x6570;&#x636E;&#x8FDB;&#x884C;&#x4EA4;&#x4E92;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; person.name
<span class="hljs-string">&quot;Tom&quot;</span>
iex&gt; Map.get(person, <span class="hljs-symbol">:name</span>)
<span class="hljs-string">&quot;Tom&quot;</span>
iex&gt; %{<span class="hljs-symbol">name:</span> name} = person
%Friends.Person{<span class="hljs-symbol">age:</span> <span class="hljs-number">11</span>, <span class="hljs-symbol">name:</span> <span class="hljs-string">&quot;Tom&quot;</span>}
iex&gt; name
<span class="hljs-string">&quot;Tom&quot;</span>
</code></pre>
<p>&#x540C;&#x6837;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x50CF;&#x5728; Elixir &#x4E2D;&#x5176;&#x4ED6;&#x7684; map &#x6216; struct &#x4E00;&#x6837;&#x53BB;&#x66F4;&#x65B0;&#x6211;&#x4EEC;&#x7684; schemas&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; %{person | <span class="hljs-symbol">age:</span> <span class="hljs-number">18</span>}
%Friends.Person{<span class="hljs-symbol">age:</span> <span class="hljs-number">18</span>, <span class="hljs-symbol">name:</span> <span class="hljs-string">&quot;Tom&quot;</span>}
iex&gt; Map.put(person, <span class="hljs-symbol">:name</span>, <span class="hljs-string">&quot;Jerry&quot;</span>)
%Friends.Person{<span class="hljs-symbol">age:</span> <span class="hljs-number">11</span>, <span class="hljs-symbol">name:</span> <span class="hljs-string">&quot;Jerry&quot;</span>}
</code></pre>
<p>&#x5728;&#x6211;&#x4EEC;&#x5173;&#x4E8E; Changesets &#x7684;&#x4E0B;&#x4E00;&#x8BFE;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x5C06;&#x5B66;&#x4E60;&#x5982;&#x4F55;&#x9A8C;&#x8BC1;&#x6211;&#x4EEC;&#x7684;&#x6570;&#x636E;&#x66F4;&#x6539;&#x4EE5;&#x53CA;&#x6700;&#x7EC8;&#x5982;&#x4F55;&#x5C06;&#x5176;&#x4FDD;&#x5B58;&#x5230;&#x6570;&#x636E;&#x5E93;&#x4E2D;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="../advanced/nerves.html" class="navigation navigation-prev " aria-label="Previous page: Nerves">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="changesets.html" class="navigation navigation-next " aria-label="Next page: Changesets">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"version":"2.3.0","title":"Basics","level":"3.1","depth":1,"next":{"title":"Changesets","level":"3.2","depth":1,"path":"lessons/ecto/changesets.md","ref":"lessons/ecto/changesets.md","articles":[]},"previous":{"title":"Nerves","level":"2.14","depth":1,"path":"lessons/advanced/nerves.md","ref":"lessons/advanced/nerves.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"lessons/ecto/basics.md","mtime":"2019-11-05T02:25:51.000Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-11-05T03:01:22.004Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

